<?php

include($GLOBALS['BIB'] . 'Pr_db/pr_historics.mex');

$error_msg['AC003'] = 'Indique uma agência';
$error_msg['AC004'] = 'Indique uma localidade.';
$error_msg['AC006'] = 'Indique o número da conta.';
$error_msg['AC007'] = 'Indique um banco.';

class ReceiverAccount {

    public function ValidateReceiverAccount($toChange) {
        global $sysTime, $SESSID, $SESSCD;
        $toChange['AID'] = $toChange['AID'];
        $toChange['RID'] = $toChange['RID'];

        if ($toChange['cb_Banco'] == '') {
            $error = 'AC007';
        }
        if (!isset($toChange['cb_Sucursal']) || $toChange['cb_Sucursal'] == '') {
            $error = 'AC003';
        }
        if (!isset($toChange['cb_CiudadSucursal'])) {
            $error = 'AC004';
        }
        if ($toChange['cb_Cuenta'] == '') {
            $error = 'AC006';
        }
        $toChange['cb_Cuenta_prx'] = isset($toChange['cb_Cuenta_prx']) ? $toChange['cb_Cuenta_prx'] : null;
        $toChange['cb_Cuenta_sux'] = isset($toChange['cb_Cuenta_sux']) ? $toChange['cb_Cuenta_sux'] : null;
        $toChange['cb_AgRegion'] = isset($toChange['cb_AgRegion']) ? $toChange['cb_AgRegion'] : 0;
        $toChange['cb_Cuenta_mod'] = isset($toChange['cb_Cuenta_mod']) ? $toChange['cb_Cuenta_mod'] : null;
        $toChange['cb_AccountNumberType'] = isset($toChange['cb_AccountNumberType']) ? $toChange['cb_AccountNumberType'] : null;
        $toChange['cb_Status'] = isset($toChange['cb_Status']) ? $toChange['cb_Status'] : null;
        if (isset($error)) {
            return $error;
        } else {
            return true;
        }
    }

    public function NewReceiverAccount($toChange) {
        global $sysTime, $SESSID, $SESSCD;
        $GLOBALS['MYT']->db_query("INSERT
			INTO beneficiarioscuentas
			(cb_Beneficiario,
				cb_Banco,
				cb_Sucursal,
				cb_CiudadSucursal,
				cb_TipoCuenta,
				cb_Cuenta,
				cb_Usuario,
				cb_Fecha,
				cb_Cuenta_prx,
				cb_Cuenta_sux,
				cb_AgRegion,
				cb_Cuenta_mod,
				cb_TipoNr)
			VALUES
			({$toChange['RID']},{$toChange['cb_Banco']},'{$toChange['cb_Sucursal']}','{$toChange['cb_CiudadSucursal']}',{$toChange['cb_TipoCuenta']},'{$toChange['cb_Cuenta']}',{$_SESSION[$SESSID]},'{$sysTime}','{$toChange['cb_Cuenta_prx']}','{$toChange['cb_Cuenta_sux']}',{$toChange['cb_AgRegion']},'{$toChange['cb_Cuenta_mod']}',{$toChange['cb_AccountNumberType']})");
        $PRAC = $GLOBALS['MYT']->db_array("SELECT cb_Id FROM beneficiarioscuentas WHERE cb_Beneficiario = {$toChange['RID']} AND cb_Usuario = {$_SESSION[$SESSID]} AND cb_Fecha = '{$sysTime}' ORDER BY cb_Id DESC LIMIT 0,1");
        $ReceiverAccountID = $PRAC[1]['cb_Id'];
        system_hs($sysTime, 'NEW_ACCOUNT', $ReceiverAccountID);
        return $ReceiverAccountID;
    }

    public function EditReceiverAccount($ReceiverAccountID, $toChange) {
        global $sysTime, $SESSID, $SESSCD;
        hs_receiveraccount($ReceiverAccountID, $sysTime);
        if ($GLOBALS['MYT']->db_query("UPDATE
            beneficiarioscuentas
            SET
            cb_Beneficiario = {$toChange['RID']},
            cb_Banco = {$toChange['cb_Banco']},
            cb_Sucursal = '{$toChange['cb_Sucursal']}',
            cb_CiudadSucursal = '{$toChange['cb_CiudadSucursal']}',
            cb_TipoCuenta = {$toChange['cb_TipoCuenta']},
            cb_Cuenta = '{$toChange['cb_Cuenta']}',
            cb_Cuenta_prx = '{$toChange['cb_Cuenta_prx']}',
            cb_Cuenta_sux = '{$toChange['cb_Cuenta_sux']}',
            cb_Usuario = {$_SESSION[$SESSID]},
            cb_Fecha = '{$sysTime}',
            cb_AgRegion = {$toChange['cb_AgRegion']},
            cb_Cuenta_mod = '{$toChange['cb_Cuenta_mod']}',
            cb_TipoNr = {$toChange['cb_AccountNumberType']},
            cb_Status = {$toChange['cb_Status']}
            WHERE cb_ID = $ReceiverAccountID") == true) {
            return $ReceiverAccountID;
        } else {
            return false;
        }
    }

}

?>
